﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AjaxPro;

using DQS.BLL;
using DQS.EL;
using DQS.Core.Log;

public partial class Operate_Conditions : DQS_Page
{    
    private readonly string NOPARAM = "NoParams";
    private decimal QueryID
    {
        get { return Decimal.Parse(Request.QueryString["queryid"].ToString()); }
    }
    private enum CondID
    {
        IF_3G = 1001,
        IF_IPNONE = 1002,
        BEGIN_DATE = 1003,
        END_DATE = 1004,
        FenBu = 1005,
        PaySum = 1006,
        IF_ThisMonth = 1007,
        IF_OutProd = 1008,
        IF_RentUser = 1009,
        BSC = 1010
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Operate_Conditions), this);
        if (!IsPostBack)
        {
            Attribute_Bind();
            Data_Init();
        }
        if (Request.QueryString["queryid"] == null)
        {
            Response.Redirect("~/Operate/Columns.aspx");
        }
    }

    private void Attribute_Bind()
    {
        Begin_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('BeginDate'),maxDate:'%y-%M-%d'});");
        End_Date.Attributes.Add("onclick", "WdatePicker({el:$dp.$('EndDate'),maxDate:'%y-%M-%d'});");
    }

    private void Data_Init()
    {
        IList<DQ_FB_Name> lists = DQ_FB_NameManager.GetAllFbName();
        FBList.DataSource = lists;
        FBList.DataTextField = "FenBu";
        FBList.DataValueField = "FenBu";
        FBList.DataBind();
        FBList.Items.Insert(0, new ListItem("全部", ""));
        AuthControl(lists);     
    }

    private void AuthControl(IList<DQ_FB_Name> lists)
    {
        DQ_USERS user = (DQ_USERS)Session["LoginUser"];
        DQ_Organization orga = DQ_OrganizationManager.GetNameByOrgaID(user.F_Orga_ID.Orga_ID);
        if (orga.Orga_Level == 4)
        { 
            foreach (DQ_FB_Name fb in lists)
            {
                if (fb.FenBu.Equals(orga.Orga_Name))
                {
                    FBList.Enabled = false;
                    FBList.SelectedValue = fb.FenBu;
                    break;
                }
            }
            if (FBList.Enabled)
            {
				OrgaList.Attributes.CssStyle.Add("display", "none");
                OrgaList.Items.Clear();                
            }
            else
            {
                OrgaList.Attributes.CssStyle.Add("display", "inline");
                Orga_DataBind(orga.Orga_Name, String.Empty);
            }
        }
        else if (orga.Orga_Level == 5)
        {
            foreach (DQ_FB_Name fb in lists)
            {
                if (fb.FenBu.Equals(orga.Sup_ID.Orga_Name))
                {
                    FBList.Enabled = false;
                    FBList.SelectedValue = fb.FenBu;
                    break;
                }
            }
            OrgaList.Attributes.CssStyle.Add("display", "inline");
            Orga_DataBind(orga.Sup_ID.Orga_Name,orga.Orga_Name);
        }        
    }

    private void Orga_DataBind(string argFb,string argBSC)
    {
        IList<DQ_FB_Name> orgas = DQ_FB_NameManager.GetAllBSCByFb(argFb);
        OrgaList.DataSource = orgas;
        OrgaList.DataTextField = "FenBu";
        OrgaList.DataValueField = "FenBu";
        OrgaList.DataBind();
        OrgaList.Items.Insert(0, new ListItem("全部", "全部"));
        if (argBSC.Length > 0)
        {
            foreach (DQ_FB_Name fb in orgas)
            {
                if (fb.FenBu.Equals(argBSC))
                {
                    OrgaList.Attributes.Add("disabled", "disabled");
                    OrgaList.Value = argBSC;
                    HidOrga.Value = argBSC;
                    break;
                }
            }
        }
    }

    protected void SaveCondition_Click(object sender, EventArgs e)
    {
        Log.Instance(this).Debug("{QueryID:"+QueryID.ToString() + ",Event:Saveing Condition}");
        IList<DQ_SETCONDITION> _objs = new List<DQ_SETCONDITION>();
        SetCond_IF3G(_objs);
        SetCond_IFiPhone(_objs);
        SetCond_BeginDate(_objs);
        SetCond_EndDate(_objs);
        SetCond_FB(_objs);
        SetCond_BSC(_objs);
        SetCond_SumPayFee(_objs);
        SetCond_IFThisMonth(_objs);
        SetCond_IFOutProd(_objs);
        SetCond_IF_RentUser(_objs);
        if (_objs.Count > 0)
        {
            DQ_SetConditionManager.InsertCondtion(_objs);
        }
        Response.Redirect("~/Operate/ShowData.aspx?queryid=" + QueryID.ToString());
    }

    private void SetCond_SumPayFee(IList<DQ_SETCONDITION> _objs)
    {
        if (SumPayFee.SelectedValue.Length > 0)
        {
            DQ_SETCONDITION setPay = new DQ_SETCONDITION();
            setPay.COND_ID = (decimal)CondID.PaySum;
            setPay.QUERY_ID = QueryID;
            setPay.PARAMS = SumPayFee.SelectedValue;
            _objs.Add(setPay);
        }
    }

    private void SetCond_FB(IList<DQ_SETCONDITION> _objs)
    {
        if (FBList.SelectedValue.Length > 0)
        {
            DQ_SETCONDITION setFb = new DQ_SETCONDITION();
            setFb.COND_ID = (decimal)CondID.FenBu;
            setFb.QUERY_ID = QueryID;
            setFb.PARAMS = FBList.SelectedValue;
            _objs.Add(setFb);
        }
    }

    private void SetCond_BSC(IList<DQ_SETCONDITION> _objs)
    {
        if (HidOrga.Value.Length > 0 && (!HidOrga.Value.Equals("全部")))
        {
            DQ_SETCONDITION setBSC = new DQ_SETCONDITION();
            setBSC.COND_ID = (decimal)CondID.BSC;
            setBSC.QUERY_ID = QueryID;
            setBSC.PARAMS = HidOrga.Value;
            _objs.Add(setBSC);
        }
    }

    private void SetCond_EndDate(IList<DQ_SETCONDITION> _objs)
    {
        if (End_Date.Text.Trim().Length > 0)
        {
            DQ_SETCONDITION setEnd = new DQ_SETCONDITION();
            setEnd.COND_ID = (decimal)CondID.END_DATE;
            setEnd.QUERY_ID = QueryID;
            setEnd.PARAMS = DateTime.Parse(End_Date.Text).AddDays(1).ToString("yyyy-MM-dd");
            _objs.Add(setEnd);
        }
    }

    private void SetCond_BeginDate(IList<DQ_SETCONDITION> _objs)
    {
        if (Begin_Date.Text.Trim().Length > 0)
        {
            DQ_SETCONDITION setBegin = new DQ_SETCONDITION();
            setBegin.COND_ID = (decimal)CondID.BEGIN_DATE;
            setBegin.QUERY_ID = QueryID;
            setBegin.PARAMS = Begin_Date.Text;
            _objs.Add(setBegin);
        }
    }

    private void SetCond_IFiPhone(IList<DQ_SETCONDITION> _objs)
    {
        if (IF_IPhone.Checked)
        {
            DQ_SETCONDITION setiph = new DQ_SETCONDITION();
            setiph.COND_ID = (decimal)CondID.IF_IPNONE;
            setiph.QUERY_ID = QueryID;
            setiph.PARAMS = "是";
            _objs.Add(setiph);
        }
    }

    private void SetCond_IFThisMonth(IList<DQ_SETCONDITION> _objs)
    {
        if (IF_ThisMonth.Checked)
        {
            DQ_SETCONDITION setiph = new DQ_SETCONDITION();
            setiph.COND_ID = (decimal)CondID.IF_ThisMonth;
            setiph.QUERY_ID = QueryID;
            setiph.PARAMS = NOPARAM;
            _objs.Add(setiph);
        }
    }

    private void SetCond_IFOutProd(IList<DQ_SETCONDITION> _objs)
    {
        if (IF_OutProd.Checked)
        {
            DQ_SETCONDITION setiph = new DQ_SETCONDITION();
            setiph.COND_ID = (decimal)CondID.IF_OutProd;
            setiph.QUERY_ID = QueryID;
            setiph.PARAMS = NOPARAM;
            _objs.Add(setiph);
        }
    }

    private void SetCond_IF_RentUser(IList<DQ_SETCONDITION> _objs)
    {
        if (IF_RentUser.Checked)
        {
            DQ_SETCONDITION setiph = new DQ_SETCONDITION();
            setiph.COND_ID = (decimal)CondID.IF_RentUser;
            setiph.QUERY_ID = QueryID;
            setiph.PARAMS = "终端合约";
            _objs.Add(setiph);
        }
    }

    private void SetCond_IF3G(IList<DQ_SETCONDITION> _objs)
    {
        if (If_3G.SelectedIndex > -1)
        {
            DQ_SETCONDITION setCon = new DQ_SETCONDITION();
            setCon.COND_ID = (decimal)CondID.IF_3G;
            setCon.QUERY_ID = QueryID;
            setCon.PARAMS = If_3G.SelectedValue;
            _objs.Add(setCon);
        }
    }

    protected void UploadFile_Click(object sender, EventArgs e)
    {
        if (ServiceIdImport.HasFile)
        {
            ServiceIdImport.PostedFile.SaveAs(Server.MapPath("~/Upload/ServiceID/") + QueryID.ToString() + ".txt");
            ServiceIdImport.Enabled = false;
            ValidatorTXTForServiceID.Enabled = false;
            UploadFile.Enabled = false;
            UploadFile.Text = "已上传";
            ServiceIDInDB();
        }
        else
        {
            Alert("未选择任何文件！");
        }
    }

    private void ServiceIDInDB()
    {       
        IList<DQ_SERVICEID> sidList = new List<DQ_SERVICEID>();
        using (StreamReader sr = new StreamReader(Server.MapPath("~/Upload/ServiceID/") + QueryID.ToString() + ".txt"))
        {
            string line = null;
            while((line = sr.ReadLine()) != null)
            {
                if(line.Trim().Length > 0)
                {
                    DQ_SERVICEID sid = new DQ_SERVICEID();
                    sid.QUERY_ID = QueryID;
                    sid.SERVICE_ID = line.Trim();
                    sidList.Add(sid);
                }
            }
        }
        if(sidList.Count > 0)
        {
            DQ_ServiceIDManager.InsertServiceIDs(sidList);
        }
    }

    [AjaxMethod]
    public IList<DQ_FB_Name> GetBscByFB(string argFb)
    {
        IList<DQ_FB_Name> lists = DQ_FB_NameManager.GetAllBSCByFb(argFb);
        if (lists.Count > 0)
        {
            DQ_FB_Name fb = new DQ_FB_Name();
            fb.FenBu = "全部";
            lists.Insert(0, fb);
        }
        else
        {
            Log.Instance(this).Error("{ErrorReason:Cann't find bsc}");
        }
        return lists;
    }
}
